using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 项目计划
    /// 版 本：V2.6.200509
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-11-16 
    /// </summary>
    internal class ProjectGanttService : DataFactory, ProjectGanttIService
    {
        /// <summary>
        /// 项目列表(分页)
        /// </summary>
        /// <returns></returns>
        public async Task<PageResult<ProjectGanttEntity>> GetList(PageInput input)
        {
            using (var db = this.Basic)
            {

                return await db.GetIQueryable<ProjectGanttEntity>().Where(m => m.Type == 1).OrderByDescending(t => t.SortCode).GetPageResultAsync(input);

            }
        }

        /// <summary>
        /// 项目列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<ProjectGanttEntity>> GetList()
        {
            using (var db = this.Basic)
            {

                return await db.GetIQueryable<ProjectGanttEntity>().Where(m => m.Type == 1).OrderByDescending(t => t.SortCode).ToListAsync();

            }
        }
        /// <summary>
        /// 任务列表
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <returns></returns>
        public async Task<List<ProjectGanttEntity>> GetTaskList(string projectId)
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<ProjectGanttEntity>().Where(m => m.Type == 2 && m.ProjectId == projectId).OrderBy(o => o.SortCode).ToListAsync();
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<ProjectGanttEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<ProjectGanttEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(ProjectGanttEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(ProjectGanttEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(ProjectGanttEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns></returns>
        public async Task<bool> IsExist(Expression<Func<ProvinceEntity, bool>> condition)
        {
            using (var db = this.Basic)
            {
                return await db.GetCountAsync(condition) > 0;
            }
        }
    }
}
